﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameLogic : MonoBehaviour {

	GameObject ballObj;
	Text timeText;
	float gameTime = 0;

	public bool gameLose = false;

	// Use this for initialization
	void Start () 
	{
		ballObj = transform.Find("Center").gameObject;
		timeText = transform.Find("Time").GetComponent<Text>();
		StartGame();
	}
	
	private float randomRadius = 400;
	void CreateRandomBall()
	{
		var randomAngle = Random.Range(0, 2 * Mathf.PI);
		var xPos = randomRadius * Mathf.Sin(randomAngle);
		var yPos = randomRadius * Mathf.Cos(randomAngle);

		/*var newObj = new GameObject();
		var img = newObj.AddComponent<Image>();
		var moveToCenter = newObj.AddComponent<MoveToCenter>();
		var collider = newObj.AddComponent<BoxCollider>();
		collider.isTrigger = true;
		collider.size = new Vector3(20, 20, 1);
		img.rectTransform.sizeDelta = new Vector2(20, 20);*/
		var newObj = CreateBall();
		var img = newObj.GetComponent<Image>();
		//var collider = newObj.GetComponent<BoxCollider>();
		Destroy(newObj.GetComponent<Rigidbody>());
		var moveToCenter = newObj.AddComponent<MoveToCenter>();
		moveToCenter.logic = this;
		moveToCenter.speed = Random.Range(20, 80);
		//collider.isTrigger = true;
		img.color = new Color(69f / 255, 137f / 255, 148f / 255);// new Color(252f / 255, 157f / 255, 154f / 255);//Color.blue;
		newObj.transform.parent = this.transform;
		newObj.transform.localPosition = new Vector3(xPos, yPos, 0);
	}

	GameObject CreateBall()
	{
		var newObj = GameObject.Instantiate(ballObj);//new GameObject();
		var img = newObj.GetComponent<Image>();
		//var collider = newObj.AddComponent<BoxCollider>();
		//newObj.AddComponent<Rigidbody>();
		//collider.isTrigger = true;
		//collider.size = new Vector3(20, 20, 1);
		var collider = newObj.GetComponent<SphereCollider>();
		collider.radius = 9.5f;
		img.rectTransform.sizeDelta = new Vector2(20, 20);
		return newObj;
	}

	float timeCount = 0;
	// Update is called once per frame

	float seperateTime = 0.2f;
	float seperateTimeCount = 0;
	int createNum = 1;

	void Update () 
	{
		CheckInput();
		if(!gameLose)
		{
			if(timeCount > seperateTime)
			{
				for(int i = 0; i < createNum; i++)
				{
					CreateRandomBall();
				}
				timeCount = 0;
			}

			if(seperateTimeCount > 10)
			{
				createNum++;
				seperateTimeCount = 0;
			}
			seperateTimeCount += Time.deltaTime;
			timeCount += Time.deltaTime;

			gameTime += Time.deltaTime;
			timeText.text = ((int)gameTime).ToString();
		}
	}

	void StartGame()
	{
		gameTime = 0;
		timeText.text = gameTime.ToString();
		SoundManager.Instance.Init();
		SoundManager.Instance.PlayBGM();
	}


	Vector3 touchPos = new Vector3(0, 0, 0);

	//检验是否操作
	bool touchScreenByPoint = false;
	void CheckInput()
	{
		#if UNITY_IPHONE || UNITY_ANDROID

		if(Input.touchCount == 1)
		{
			touchScreenByPoint = true;
			touchPos = Input.touches[0].position;
		}
		else if(Input.touchCount == 0)
		{
			if(touchScreenByPoint)
			{
                touchScreenByPoint = false;

				var pos = touchPos;
				var center = new Vector3(Screen.width / 2, Screen.height / 2);
				var dir = Vector3.Normalize(pos - center);

				var ball = CreateBall();
				ball.name = "Click Create";
				ball.transform.parent = this.transform;
				ball.GetComponent<Image>().color = new Color(252f / 255, 157f / 255, 154f / 255);
				ball.transform.localPosition = Vector3.zero;
				var moveWithDir = ball.AddComponent<MoveWithDir>();
				moveWithDir.dir = dir;
				//SoundManager.Instance.PlaySoundWithID(0);
			}
		}
        #endif

		#if UNITY_EDITOR || UNITY_WIN
		if(Input.GetMouseButtonDown(0))
		{
			var pos = Input.mousePosition;
			var center = new Vector3(Screen.width / 2, Screen.height / 2);
			var dir = Vector3.Normalize(pos - center);

			var ball = CreateBall();
			ball.name = "Click Create";
			ball.transform.parent = this.transform;
			ball.GetComponent<Image>().color = new Color(252f / 255, 157f / 255, 154f / 255);
			ball.transform.localPosition = Vector3.zero;
			var moveWithDir = ball.AddComponent<MoveWithDir>();
			moveWithDir.dir = dir;
			//SoundManager.Instance.PlaySoundWithID(0);

			/*var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if(Physics.Raycast(ray, out hit, 1000))
			{
				var ball = CreateBall();
				ball.name = "Click Create";
				ball.transform.parent = this.transform;
				ball.transform.position = hit.point;
				ball.GetComponent<Image>().color = Color.red;
			}*/

			/*var ball = CreateBall();
			ball.name = "Click Create";
			ball.transform.parent = this.transform;
			ball.transform.localPosition = Input.mousePosition;
			ball.GetComponent<Image>().color = Color.red;*/
		}
		#endif
	}
}
